package Top200;

/**
 * @author zhangmin
 * @create 2022-02-17 10:53
 * 以每个字符为中心找最长回文字串
 */
public class longestPalindrome05 {
    String getPalindrome(String s,int left,int right){
        while (left>=0&&right<s.length()&&s.charAt(left)==s.charAt(right)){
            left--;right++;
        }
        return s.substring(left+1,right);
    }

    public String longestPalindrome(String s) {
        int n=s.length();
        String res="";
        for (int i = 0; i < n; i++) {
            String s1 = getPalindrome(s, i, i);
            String s2 = getPalindrome(s, i, i+1);
            res=s1.length()>res.length()?s1:res;
            res=s2.length()>res.length()?s2:res;
        }
        return res;
    }
}
